package com.stars.empoly.vmodel

import android.app.Application
import android.graphics.Color
import android.text.Editable
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.hwangjr.rxbus.RxBus
import com.stars.empoly.activity.PlaceOrderActivity
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.model.CityList
import com.stars.empoly.model.ServiceList
import com.stars.empoly.model.User
import com.stars.empoly.utils.PopuTipsUtils
import com.stars.empoly.utils.net.NetEngine
import com.stars.empoly.view.SimpleTextWatcher
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

/**
 *
 *on 2020/9/25
 */
class TechListVModel(application: Application) : BRViewModel<BRRepository>(application) {


    var cityList=MutableLiveData<List<CityList>>()

    var cityId:String?=null

    var technicianServiceId:String?=""

    var keyword:String?=null

    var oldText = ""
    var textWatcher = object : SimpleTextWatcher() {
        override fun afterTextChanged(s: Editable?) {
            super.afterTextChanged(s)
            var content = s.toString()
            if (!TextUtils.isEmpty(oldText) && content == oldText) {
                return
            }
            keyword = content
            oldText = content
            //刷新
            firstReq()

        }

    }

    fun getCityList(){
        viewModelScope.launch{

            var it=repository().lreq { NetEngine.service.cityList() }

            if(it.ok()){
                cityList.value=it.data
            }
        }

    }


    var index=MutableLiveData<Int>()
    var bean=MutableLiveData<ServiceList>()

    fun toTechnician(id:String?){
        viewModelScope.launch {
            var it= NetEngine.service.platformServiceList(id,null,null)
            if(it.ok()){
              //  bean.value=it.data

                var data =it.data as ServiceList
                for(item in data.content){
                    if(item.id==technicianServiceId){
                        //到下单界面
                        item.choseNumber="1"
                        var choseData=ArrayList<ServiceList.ContentBean>()
                        choseData.add(item)
                        UiSwitch.bundle(repository().context, PlaceOrderActivity::class.java, B().putString("id",data.technicianInfo.technicianId)
                            .putString("name",data.technicianInfo.realName)
                            .putString("list", Gson().toJson(choseData)).ok())

                    }
                }

            }

        }



    }

    fun toFollow( id:String?,position:Int?){

        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.follow(id) }
            if(it.ok(true)){
                index.value=position
            }
        }

    }





    override fun apiFunc(): suspend () -> IResp<*> {

        //todo:
        return { NetEngine.service.technicianList(keyword,cityId,technicianServiceId,MMKV.defaultMMKV().getString("lat",null), MMKV.defaultMMKV().getString("lng",null),10,repository().page) }


    }

}
